/**
 * 
 */
package com.ekingstar.framework.action;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.conversion.annotations.Conversion;
import com.ekingstar.framework.domain.PublicCode;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.IUserService;
import com.ekingstar.framework.service.UserExistException;

/**
 * @author GQ
 *
 */

@SuppressWarnings({ "unchecked", "serial" })
@Action(name="userinfo",namespace="/user")
@Results( {
		@Result(name = "success", value = "/user/success.jsp"),
		@Result(name = "error", value = "/error.jsp"),
		@Result(name = "nologin", value = "/user/nologin.jsp"),
		@Result(name = "info",value="/info.jsp")
		//@Result(name = "input", value = "userinfo", type = ActionChainResult.class, params = {
		//		"namespace", "/user", "method", "input" })
})
@Conversion
public class UserinfoAction extends ActionSupport implements Preparable{
	
	
	private IUserService userService;
	private User user;
	private Map universitys;
	private Map userTypes;
	
	private String userType;

	private List<PublicCode> gzlbList;
	
	public String input() {
		this.user = UserUtil.getCurrentUser();
		this.user = this.userService.viewUser(user.getId());
		/*this.userType = user.getUserType();
		if("EDU".equals(this.userType)){
			this.userType = "1";
		}
		*/
		if (this.user.getName().equals("") || this.user.getName() == null
				|| this.user.getName().trim().equals("roleAnonymous")) {
			return "nologin";
		}
		return "input";
	}

	public String sub() {
		try {
			this.userService.changUserInfo(this.user);
		} catch (UserExistException e) {
			this.addFieldError(ERROR,e.getMessage());
			this.user = UserUtil.getCurrentUser();
			return "input";
		}
		HttpServletRequest request  = ServletActionContext.getRequest();
		request.setAttribute("info", "个人信息已更新，建议重新登陆系统！");
		request.setAttribute("ljname", "重新登陆");
		request.setAttribute("ljdz", "/j_spring_security_logout");
		return "info";
		//return "success";
	}
	public void prepare() throws Exception {
		if (userTypes == null) {
			userTypes = new LinkedHashMap();
			User.Type[] uts = User.Type.values();
			for (int i = 0 ;i  < uts.length ; i++){
				userTypes.put(uts[i].name(), uts[i].toString());
			}
		}
	}
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Map getUniversitys() {
		return universitys;
	}

	public void setUniversitys(Map universitys) {
		this.universitys = universitys;
	}

	public Map getUserTypes() {
		return userTypes;
	}

	public void setUserTypes(Map userTypes) {
		this.userTypes = userTypes;
	}

	

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public List<PublicCode> getGzlbList() {
		return gzlbList;
	}

	public void setGzlbList(List<PublicCode> gzlbList) {
		this.gzlbList = gzlbList;
	}
}
